﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.QLMS.Entity;
using ZDSoft.QLMS.Entity.SystemSetting;
using ZDSoft.QLMS.Web.Helpers;
using NHibernate.Criterion;
using ZDSoft.TES.Entity.SystemEntity;

namespace ZDSoft.QLMS.Web.Controllers
{
    public class OrganizationController : ControllerBase
    {
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetAllOrg()
        {
            IList<Organization> list = new Organization().FindAll();
            IList<ZTreeHelper<Organization>.ZTreeNode> tree = new List<ZTreeHelper<Organization>.ZTreeNode>();
            foreach (Organization entity in list)
            {
                if (entity.Parent == null)
                {
                    tree.Add(GetNodeDeatil(entity));
                }
            }
            return this.Json(tree, JsonRequestBehavior.AllowGet);
        }

        public ZTreeHelper<Organization>.ZTreeNode GetNodeDeatil(Organization entity)
        {
            ZTreeHelper<Organization>.ZTreeNode node = new ZTreeHelper<Organization>.ZTreeNode();
            if (entity.Children.Count == 0)
            {
                node = SetNode(entity);
            }
            else
            {
                node = SetNode(entity);
                node.children = new List<ZTreeHelper<Organization>.ZTreeNode>();
                foreach (Organization chi in entity.Children)
                {
                    node.children.Add(GetNodeDeatil(chi));
                }
            }
            return node;
        }

        public ZTreeHelper<Organization>.ZTreeNode SetNode(Organization entity)
        {
            ZTreeHelper<Organization>.ZTreeNode node = new ZTreeHelper<Organization>.ZTreeNode();
            node.id = entity.ID;
            node.tld = entity.IsActive.ToString();
            node.isParent = true;
            node.pid = 0;
            node.icon = "../../Content/img/diy/1_close.png";
            node.name = entity.OrganizationName;
            return node;
        }

        public string GetEntiyID(string name)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            queryConditions.Add(Expression.Eq("OrganizationName", name));
            IList<Organization> list = new Organization().Find(queryConditions);
            string result = list[0].ID.ToString();
            return result;
        }

        public void Delete(int id)
        {
            Organization org = new Organization().FindByID(id);
            if (org.Children.Count == 0)
            {
                new Organization().Delete(id);
            }
            else
            {
                foreach (Organization i in org.Children)
                {
                    Delete(i.ID);
                }
                new Organization().Delete(id);
            }
        }

        public void Opreate(string isActive, string id, string name, string pId, string level)
        {
            if (id == "0")
            {
                Organization entity = new Organization();
                entity.OrganizationName = name;
                if (level == "0")
                {
                    entity.OrganizationType = OrganizationType.系部;
                }
                else if (level == "1")
                {
                    entity.OrganizationType = OrganizationType.教研室;
                }
                else if (level == "2")
                {
                    entity.OrganizationType = OrganizationType.班级;
                }
                if (pId != "null")
                {
                    entity.Parent = new Organization().FindByID(int.Parse(pId));
                }
                if (isActive != "1")
                {
                    entity.IsActive = false;
                }
                else
                {
                    entity.IsActive = true;
                }
                new Organization().Create(entity);
            }
            else
            {
                Organization entity = new Organization().FindByID(int.Parse(id));
                entity.OrganizationName = name;
                if (isActive != "1")
                {
                    entity.IsActive = false;
                }
                else
                {
                    entity.IsActive = true;
                }
                new Organization().Edit(entity);
            }
        }
    }
}
